package Model;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Arrays;
import java.util.Date;
import java.util.Enumeration;

/**
 * 异常发送类
 */
public class ExceptionMqModel {
    @Override
    public String toString() {
        return "ExceptionMqModel{" +
                "serviceName='" + serviceName + '\'' +
                "ip='" + ip + '\'' +
                ", port='" + port + '\'' +
                ", version='" + version + '\'' +
                ", exceptionName='" + exceptionName + '\'' +
                ", Author='" + Author + '\'' +
                ", date=" + date +
                ", remark=" + remark +
                ", stackTraceElementArrays=" + Arrays.toString(stackTraceElementArrays) +
                '}';
    }

    private String getIpAddress() {
        try {
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip;
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = allNetInterfaces.nextElement();
                if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
                    continue;
                } else {
                    Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        ip = addresses.nextElement();
                        if (ip != null && ip instanceof Inet4Address) {
                            return ip.getHostAddress();
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.err.println("IP地址获取失败" + e.toString());
        }
        return null;
    }

    /**
     * @param serviceName 发送方微服务名
     * @param port        发送方端口
     * @param e           异常对象
     */
    public ExceptionMqModel(String serviceName, String port, String version, String remark, String authorityName, Exception e) {
        this.ip = this.getIpAddress();
        this.serviceName = serviceName;
        this.port = port;
        this.version = version;
        this.remark = remark;
        this.date = new Date();
        if (e != null) {
            this.setAuthor(authorityName);
            this.exceptionName = e.getClass().getName();
            StackTraceElement[] stackTrace = e.getStackTrace();
            if (stackTrace != null && stackTrace.length > 0) {
//                int index = 0;
//                stackTraceElementArrays = new StackTraceElementArray[index + 1];
//                StackTraceElementArray stackTraceElementArray = new StackTraceElementArray();
//                stackTraceElementArray.setIndex(index);
//                stackTraceElementArray.setClassName(stackTrace[index].getClassName());
//                stackTraceElementArray.setFileName(stackTrace[index].getFileName());
//                stackTraceElementArray.setLineNumber(stackTrace[index].getLineNumber());
//                stackTraceElementArray.setMethodName(stackTrace[index].getMethodName());
//                stackTraceElementArrays[index] = stackTraceElementArray;
                stackTraceElementArrays = new StackTraceElementArray[stackTrace.length];
                for (int i = 0; i < stackTrace.length; i++) {
                    StackTraceElementArray stackTraceElementArray = new StackTraceElementArray();
                    stackTraceElementArray.setIndex(i);
                    stackTraceElementArray.setClassName(stackTrace[i].getClassName());
                    stackTraceElementArray.setFileName(stackTrace[i].getFileName());
                    stackTraceElementArray.setLineNumber(stackTrace[i].getLineNumber());
                    stackTraceElementArray.setMethodName(stackTrace[i].getMethodName());
                    stackTraceElementArrays[i] = stackTraceElementArray;
                }
            }
        }
    }

    private String serviceName;
    private String port;
    private String version;
    private String remark;
    private String ip;

    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public void setExceptionName(String exceptionName) {
        this.exceptionName = exceptionName;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public void setStackTraceElementArrays(StackTraceElementArray[] stackTraceElementArrays) {
        this.stackTraceElementArrays = stackTraceElementArrays;
    }

    public String getServiceName() {
        return serviceName;
    }

    public String getPort() {
        return port;
    }

    public String getExceptionName() {
        return exceptionName;
    }

    public String getAuthor() {
        return Author;
    }

    public Date getDate() {
        return date;
    }

    public StackTraceElementArray[] getStackTraceElementArrays() {
        return stackTraceElementArrays;
    }

    private String exceptionName;
    private String Author;

    public void setAuthor(String author) {
        int by = author.indexOf("by");
        if (by > -1) {
            Author = author.replace("/", "")
                    .replace("by", "")
                    .trim();
        }
        else
            Author = author;
    }

    private Date date;
    private StackTraceElementArray[] stackTraceElementArrays;

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public class StackTraceElementArray {
        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }

        public String getClassName() {
            return className;
        }

        public void setClassName(String className) {
            this.className = className;
        }

        public String getFileName() {
            return fileName;
        }

        public void setFileName(String fileName) {
            this.fileName = fileName;
        }

        public String getMethodName() {
            return methodName;
        }

        public void setMethodName(String methodName) {
            this.methodName = methodName;
        }

        public int getLineNumber() {
            return lineNumber;
        }

        public void setLineNumber(int lineNumber) {
            this.lineNumber = lineNumber;
        }

        private int index;
        private String className;
        private String fileName;
        private String methodName;
        private int lineNumber;
    }
}
